package com.musifind.activities;

import java.text.ParseException;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.ImageView;

import com.fizy.client.controllers.FizyControllers;
import com.fizy.client.objects.Album;
import com.fizy.client.objects.Artist;
import com.fizy.client.objects.Search;
import com.fizy.client.objects.Track;
import com.musifind.adapters.SearchAdapterAlbums;
import com.musifind.adapters.SearchAdapterArtists;
import com.musifind.adapters.SearchAdapterTracks;
import com.musifind.common.DateTimeUtils;
import com.musifind.models.SearchItems;

public class BaseFragment extends Fragment {
	static SearchAdapterTracks searchAdapterTracks;
	static ArrayList<SearchItems> searchArrayTracks = new ArrayList<SearchItems>();

	static SearchAdapterAlbums searchAdapterAlbums;
	static ArrayList<SearchItems> searchArrayAlbums = new ArrayList<SearchItems>();

	static SearchAdapterArtists searchAdapterArtists;
	static ArrayList<SearchItems> searchArrayArtists = new ArrayList<SearchItems>();

	static ImageView imgLogo;
	static String searchKey;
	private ProgressDialog progressDialog = null;

	// public void init() {
	// searchAdapterTracks = new SearchAdapterTracks(getActivity(), R.layout.search_list_items_row_tracks,
	// searchArrayTracks);
	// searchAdapterAlbums = new SearchAdapterAlbums(getActivity(), R.layout.search_list_items_row_album,
	// searchArrayAlbums);
	// searchAdapterArtists = new SearchAdapterArtists(getActivity(), R.layout.search_list_items_row_artist,
	// searchArrayArtists);
	//
	// getActivity().runOnUiThread(new Runnable() {
	// public void run() {
	// searchAdapterTracks.notifyDataSetChanged();
	// searchAdapterAlbums.notifyDataSetChanged();
	// searchAdapterArtists.notifyDataSetChanged();
	// }
	// });
	// }

	public void getResults(final String searchKey) {
		if (searchKey != null && !searchKey.isEmpty()) {
			FizyControllers controllers = new FizyControllers();
			Search search = controllers.search(searchKey);
			Log.i("Search Query", searchKey);

			// TRACK
			searchArrayTracks.clear();
			for (Track track : search.getTracks()) {
				String strAlbumCover = track.getAlbum().getCover_medium();
				String strTrackName = track.getName();
				String strArtistName = track.getArtist().getName();
				String strAlbumName = track.getAlbum().getName();
				String strAlbumYear = track.getAlbum().getRelease_date();
				String strAlbumYearNew = "";
				try {
					strAlbumYearNew = DateTimeUtils.stringToDate(strAlbumYear);
				} catch (ParseException e) {
					Log.e("Date Conversion Problem", "Date hasn't parsed");
				}
				searchArrayTracks.add(new SearchItems(track.getId(), strAlbumCover, strTrackName, strArtistName,
						strAlbumName, " (" + strAlbumYearNew + ")"));
			}

			// ALBUM
			searchArrayAlbums.clear();
			for (Album album : search.getAlbums()) {
				String strAlbumCover = album.getCover_medium();
				String strArtistName = album.getArtist().getName();
				String strAlbumName = album.getName();
				String strAlbumYear = album.getRelease_date();
				String strAlbumYearNew = "";
				try {
					strAlbumYearNew = DateTimeUtils.stringToDate(strAlbumYear);
				} catch (ParseException e) {
					Log.e("Date Conversion Problem", "Date hasn't parsed");
				}
				searchArrayAlbums.add(new SearchItems(album.getId(), strAlbumCover, strAlbumName, " ("
						+ strAlbumYearNew + ")", strArtistName));
			}

			// ARTIST
			searchArrayArtists.clear();
			for (Artist artist : search.getArtists()) {
				Log.i("artist name: ", artist.getName());
				String strArtistName = artist.getName();
				searchArrayArtists.add(new SearchItems(artist.getId(), strArtistName));
			}
		} else {
			searchArrayTracks.clear();
			searchArrayAlbums.clear();
			searchArrayArtists.clear();
		}

		getActivity().runOnUiThread(new Runnable() {
			public void run() {
				if (searchAdapterTracks != null)
					searchAdapterTracks.notifyDataSetChanged();
				if (searchAdapterAlbums != null)
					searchAdapterAlbums.notifyDataSetChanged();
				if (searchAdapterArtists != null)
					searchAdapterArtists.notifyDataSetChanged();
			}
		});
	}

	public void callAsync(String searchKey) {
		this.searchKey = searchKey;
		new SearchAsyncTask().execute();
	}

	public class SearchAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(getActivity());
			progressDialog.setTitle("Aranıyor");
			progressDialog.setMessage("Lütfen bekleyiniz...");
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			getResults(searchKey);
			return null;
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}
	}
}
